//
//  WBBaseViewController.swift
//  CSWeibo
//
//  Created by 白长松 on 16/6/29.
//  Copyright © 2016年 白长松. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {
    
    ///用户信息字典
    var visitorInfoDictionary: [String: String]?
    
    ///用户登录标记
    var isLogon = false
    
    ///定义上拉刷新的状态
    var isPullUp = false
    
    ///定义上拉刷新控件
    var refreshControl: UIRefreshControl?
    
    ///定义表格视图
    var tableView: UITableView?
    
    ///定义navigationbar属性
    lazy var navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main().bounds.width, height: 64))
    //定义导航栏条目属性
    lazy var navItem: UINavigationItem = UINavigationItem()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loadData()
    }
    
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    
    func loadData() {
        print("加载数据")
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension WBBaseViewController {
    private func setupUI() {
        view.backgroundColor = UIColor.cz_random()
        
        setupNavigationBar()
        isLogon ? setupTableView() : setupVisitorView()
    }
    
    func setupNavigationBar() -> () {
        //添加导航条
        view.addSubview(navBar)
        //添加导航条条目
        navBar.items = [navItem]
        
        //设置navbar的整个背景渲染颜色
        navBar.barTintColor = UIColor.cz_color(withHex: 0xf6f6f6)
        //设置navbar的字体颜色
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray()]
        //设置系统按钮的字体颜色
        navBar.tintColor = UIColor.orange()
    }
    
    func setupTableView() {
        
        automaticallyAdjustsScrollViewInsets = false
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navBar)
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
        tableView?.contentInset = UIEdgeInsetsMake(navBar.bounds.height, 0, tabBarController?.tabBar.bounds.height ?? 49, 0)
        
        //添加下拉刷新控件
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    func setupVisitorView() {
        let visitorView = WBVisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navBar)
        visitorView.visitorInfo = visitorInfoDictionary
        
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(login))
    }
    
}

//MARK: - UITableViewDataSource,UITableViewDelegate
extension WBBaseViewController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        
        let section = tableView.numberOfSections - 1
        let row = tableView.numberOfRows(inSection: section) - 1
        
        if section < 0 || row < 0 {
            return
        }
        if indexPath.section == section && indexPath.row == row {
            print("上拉刷新")
            isPullUp = true
            
            //开始刷新
            loadData()
        }
    }
}

// MARK: - 访客视图按钮监听方法
extension WBBaseViewController {
    
    @objc func register() {
        print("register")
    }
    
    @objc func login() {
        print("login")
    }
}

